package edu.nf.relaxenjoy.web.controller.app;

import edu.nf.relaxenjoy.common.result.ResultVO;
import edu.nf.relaxenjoy.common.validator.ValidatorUtils;
import edu.nf.relaxenjoy.common.validator.group.AddGroup;
import edu.nf.relaxenjoy.common.validator.group.DefaultGroup;
import edu.nf.relaxenjoy.domain.entity.User;
import edu.nf.relaxenjoy.service.AuthService;
import edu.nf.relaxenjoy.web.controller.BaseController;
import edu.nf.relaxenjoy.domain.dto.AuthUserDTO;
import edu.nf.relaxenjoy.domain.dto.UserOtherDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhu
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthUserController extends BaseController {

    private final AuthService authService;

    /**
     * 用户登录
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("login")
    public ResultVO<?> login(@RequestBody AuthUserDTO authUserDTO) {
        Map<String, Object> map = authService.login(authUserDTO);
        return success(map);
    }

    /**
     * 使用验证码登录
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("loginByCode")
    public ResultVO<?> loginByCode(@RequestBody AuthUserDTO authUserDTO) {
        Map<String, Object> map = authService.loginByCode(authUserDTO);
        return success(map);
    }


    /**
     * 根据用户的token信息得到当前用户
     *
     * @param token token信息
     * @return 用户类
     */
    @RequestMapping("getUserInfoByToken")
    public ResultVO<?> getUserInfoByToken(String token) {
        User user = authService.getUserInfoByToken(token);
        return success(user);
    }


    /**
     * 用户注册
     *
     * @param authUserDTO 前台传递用户信息
     */
    @RequestMapping("register")
    public ResultVO<?> register(@RequestBody AuthUserDTO authUserDTO) {
        ValidatorUtils.validateEntity(authUserDTO, AddGroup.class, DefaultGroup.class);
        Map<String, Object> data = authService.regist(authUserDTO);
        return success(data);

    }

    /**
     * 用户是否注册
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("isRegist")
    public ResultVO<?> isRegist(@RequestBody AuthUserDTO authUserDTO) {
        boolean isRegist = authService.isRegist(authUserDTO);
        return success(isRegist);

    }


    /**
     * 退出登录
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("loginOut")
    public ResultVO<?> loginOut(@RequestBody AuthUserDTO authUserDTO) {
        authService.loginOut(authUserDTO);
        return success();
    }


    /**
     * 校验
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("check")
    public ResultVO<?> check(@RequestBody AuthUserDTO authUserDTO) {
        Map<String, Object> map = authService.check(authUserDTO);
        return success(map);
    }

    /**
     * 修改密码
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("updatePassword")
    public ResultVO<?> updatePassword(@RequestBody AuthUserDTO authUserDTO) {
        ValidatorUtils.validateEntity(authUserDTO, DefaultGroup.class);
        Boolean flag = authService.updatePassword(authUserDTO);
        return success(flag);
    }


    /**
     * 第三方登录
     *
     * @param userOtherDTO
     * @return
     */
    @RequestMapping("/otherLogin")
    public ResultVO<?> otherLogin(@RequestBody UserOtherDTO userOtherDTO) {
        Map<String, Object> map = authService.otherLogin(userOtherDTO);
        return success(map);
    }

    /**
     * (已废弃)刷新token
     *
     * @param authUserDTO
     * @return
     */
    @RequestMapping("refreshToken")
    public ResultVO<?> refreshToken(@RequestBody AuthUserDTO authUserDTO) {
        Map<String, Object> map = authService.refreshToken(authUserDTO);
        return success(map);
    }
}
